package com.badshun.function;

import com.badlogic.gdx.*;
import android.content.*;
import com.badlogic.gdx.graphics.glutils.*;
import com.badlogic.gdx.graphics.*;
import android.os.*;

public class FunctionDrawer extends ApplicationAdapter {
	private Bundle bundle;
	
	private ShapeRenderer sr_space;
	private ShapeRenderer sr_z;
	private ShapeRenderer sr_f;
	
	private float x1Z;
	private float y1Z;
	private float x2Z;
	private float y2Z;
	
	private float x1F;
	private float y1F;
	private float x2F;
	private float y2F;
	
	private final float STEP = 0.5f;
	
	public FunctionDrawer(Bundle bundle) {
		this.bundle = bundle;
	}
	
	@Override
	public void create() {
		sr_space = new ShapeRenderer();
		sr_z = new ShapeRenderer();
		sr_f = new ShapeRenderer();
	}

	@Override
	public void render() {
		sr_space.begin(ShapeRenderer.ShapeType.Line);
		sr_space.setColor(Color.RED);
		sr_space.line(0,Gdx.graphics.getHeight() / 2,Gdx.graphics.getWidth(),Gdx.graphics.getHeight() / 2);
		sr_space.setColor(Color.BLUE);
		sr_space.line(Gdx.graphics.getWidth() / 2,0,Gdx.graphics.getWidth() / 2,Gdx.graphics.getHeight());
		sr_space.end();
		
		switch(bundle.getInt("TYPE")) {
			case 0:
				x1Z = x2Z;
				y1Z = y2Z;

				x2Z = x1Z + STEP;
				y2Z = bundle.getFloat("k") * x2Z;
				
				x1F = x2F;
				y1F = y2F;

				x2F = x1F - STEP;
				y2F = bundle.getFloat("k") * x2F;
				
				break;
			case 1:
				x1Z = x2Z;
				y1Z = y2Z;

				x2Z = x1Z + STEP;
				y2Z = bundle.getFloat("k") * x2Z + bundle.getFloat("b");

				x1F = x2F;
				y1F = y2F;

				x2F = x1F - STEP;
				y2F = bundle.getFloat("k") * x2F + bundle.getFloat("b");
				
				break;
			case 2:
				x1Z = x2Z;
				y1Z = y2Z;

				x2Z = x1Z + STEP;
				y2Z = bundle.getFloat("a") * x2Z * x2Z + bundle.getFloat("b") * x2Z + bundle.getFloat("c");

				x1F = x2F;
				y1F = y2F;

				x2F = x1F - STEP;
				y2F = bundle.getFloat("a") * x2F * x2F + bundle.getFloat("b") * x2F + bundle.getFloat("c");
				
				break;
			case 3:
				x1Z = x2Z;
				y1Z = y2Z;

				x2Z = x1Z + STEP;
				y2Z = bundle.getFloat("a") * (x2Z - bundle.getFloat("h")) * (x2Z - bundle.getFloat("h")) + bundle.getFloat("b");

				x1F = x2F;
				y1F = y2F;

				x2F = x1F - STEP;
				y2F = bundle.getFloat("a") * (x2F - bundle.getFloat("h")) * (x2F - bundle.getFloat("h")) + bundle.getFloat("b");
				
				break;
		}
		
		sr_z.begin(ShapeRenderer.ShapeType.Line);
		sr_z.line(x1Z + Gdx.graphics.getWidth() / 2,y1Z + Gdx.graphics.getHeight() / 2,x2Z + Gdx.graphics.getWidth() / 2,y2Z + Gdx.graphics.getHeight() / 2);
		sr_z.end();
		
		sr_f.begin(ShapeRenderer.ShapeType.Line);
		sr_f.line(x1F + Gdx.graphics.getWidth() / 2,y1F + Gdx.graphics.getHeight() / 2,x2F + Gdx.graphics.getWidth() / 2,y2F + Gdx.graphics.getHeight() / 2);
		sr_f.end();
	}
}
